package com.sskj.finance.ui.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.WidgetAccess;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.finance.R;
import com.sskj.finance.R2;
import com.sskj.finance.presenter.AppointmentBuyActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.NumberUtil;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.FINAN_APPOINTMENT_BUY)//预约买入
public class AppointmentBuyActivity extends BaseActivity<AppointmentBuyActivityPresenter> {
    @Autowired
    String id;
    @Autowired
    String name;
    @Autowired
    String date;
    @Autowired
    String min;
    @Autowired
    String rate;
    @BindView(R2.id.tvName)
    TextView tvName;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @BindView(R2.id.tvProfile)
    TextView tvProfile;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.tvGetCheckCode)
    TextView tvGetCheckCode;
    @BindView(R2.id.checkbox)
    CheckBox checkbox;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private Disposable timeDis;

    @Override
    protected int getLayoutId() {
        return R.layout.finan_activity_appointment_buy;
    }

    @Override
    public AppointmentBuyActivityPresenter getPresenter() {
        return new AppointmentBuyActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity1_j));
        tvName.setText(name);
        etNum.setHint(String.format(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity2_j), min, min));
        SimpleDateFormat sf = new SimpleDateFormat(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity3_j));
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_YEAR, 1);
        String format = sf.format(instance.getTime());
        tvDesc.setText(String.format(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity4_j), date));
        ClickUtil.click(btSubmit, () -> {
            if (checkParam()) {
                mPresenter.createOrder(id, etNum.getText().toString(), etCode.getText().toString());
            }
        });
        ClickUtil.click(tvGetCheckCode, () -> {
            tvGetCheckCode.setEnabled(false);
            mPresenter.sendCode();
        });
        RxTextView.afterTextChangeEvents(etNum)
                .subscribe(textViewAfterTextChangeEvent -> {
                    String s = textViewAfterTextChangeEvent.editable().toString();
                    if (TextUtils.isEmpty(s)) {
                        tvProfile.setText("");
                        return;
                    }
                    BigDecimal multiply = new BigDecimal(rate).multiply(new BigDecimal("0.01")).multiply(new BigDecimal(s));
                    tvProfile.setText(NumberUtil.keepNoZero(multiply.toPlainString()));

                });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(etNum.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity5_j));
            return false;
        }
        if (Integer.valueOf(etNum.getText().toString()) % Double.valueOf(min).intValue() != 0) {
            ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity6_j), min));
            return false;
        }
        if (TextUtils.isEmpty(etCode.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity7_j));
            return false;
        }
        if (!checkbox.isChecked()) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity8_j));
            return false;
        }
        return true;
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime()
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        tvGetCheckCode.setText(aLong + App.INSTANCE.getString(R.string.finan_appointmentBuyActivity9_j));
                    } else {
                        tvGetCheckCode.setText(App.INSTANCE.getString(R.string.finan_appointmentBuyActivity10_j));
                        tvGetCheckCode.setEnabled(true);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void sendCheckCodeFail() {
        tvGetCheckCode.setEnabled(true);
    }

    public void buySuccess() {
        ARouter.getInstance().build(RConfig.FINAN_MY_ORDER).navigation();
        finish();
    }
}
